<?php
Yii::import('application.components.auctionCache.class.AuctionCacheListHash');
Yii::import('dao.auction.AuctionLiveDao');
Yii::import('application.components.Log');
class AuctionCacheData extends CRedisModel{
    public $id = 0;
    public $start_time;
    public $end_time;
    public $winner_price_increment;
    public $winner_price;
    public $winner_time;
    public $winner_name;
    public $winner_id;
    public $status;
    public $category_id;
    public $winner_avatar;
    public $_log;
    
    public $title;
    public $featured;
    protected  $_value_price;
    protected $_images;
    protected $primaryAttribute = 'id';
    
    protected $_indexListHash;
    
    const AUTION_CACHE_DATA_PRE = 'auction_cache_data';
    const AUTION_CACHE_MARK_PRE = 'auction_cache_mark';
        
    protected function getDataKey($pk = null){
        if ($pk === null){
            $pk = $this->{$this->primaryAttribute};
        }
        return self::AUTION_CACHE_DATA_PRE.':'.$pk;
    }
    
    protected function getMarkKey($pk = null){
        if ($pk === null){
            $pk = $this->{$this->primaryAttribute};
        }
        return self::AUTION_CACHE_MARK_PRE.':'.$pk;
    }
    
    public function getPrimaryAttribute(){
        return $this->primaryAttribute;
    }
    
    public function init(){
        parent::init();
        $this->build();
    }
    
    
    public function setImages($images){
        if(!empty($images)){
            if(is_array($images)){
                $images = array_shift($images);
            }
        }else{
            $images = null;
        }
        return $this->_images = $images;
    }

    public function getImages(){
        return $this->_images;
    }
    
    /**
     *     add attribute like this 
     */
    public function setValue_price($price){
        if (!empty($price)){
            $this->_value_price=$price;
        }else {
            $price=null;
        }
        return $this->_value_price=$price;
    }
    
    public function getValue_price(){
        return $this->_value_price;
    }
    
    public function getAttributes($names = null){
        $attributes = parent::getAttributes($names);
        $attributes['images'] = $this->images;
        $attributes['value_price']=$this->value_price;
        $attributes['end_time']=$this->end_time;
        return $attributes;
    }

    public function rules(){
        return array(
            array('id,start_time,end_time,winner_price_increment,winner_price,winner_time,winner_name,winner_id,category_id,status,title,featured,images,winner_avatar,value_price','safe')
        );
    }
    
    protected function build(){
        $indexList = $this->indexList();
        if (!empty($indexList)){
            foreach ($indexList as $indexName => $indexConfig){
                $class = Yii::import($indexConfig['class']);
                $list = new $class();
                $list->build($indexConfig);
                $this->getIndexList()->addList($list);
            }
        }
    }
    
    public function getIndexList(){
        if ($this->_indexListHash === null){
            $this->_indexListHash = new AuctionCacheListHash();
        }
        return $this->_indexListHash;
    }
    
    public function indexList(){
        return array(
            'live'      => array(
                'class' => 'application.components.auctionCache.AuctionLiveList',
                'scoreAttribute'  => 'end_time',
                'indexAttribute'  => $this->primaryAttribute
            ),  
            'category'  => array(
                'class' => 'application.components.auctionCache.AuctionCategoryList',
                'scoreAttribute'    => 'end_time',
                'indexAttribute'    => $this->primaryAttribute,
                'categoryAttribute' => 'category_id',
            ),
        );
    }
    
    public function create(){        
        $this->redis()->hMset($this->getDataKey(),$this->attributes);
        //$this->getIndexList()->addAuctionItem($this);
        $this->redis()->set($this->getMarkKey(),time());
        return $this;
    }
    
    public function delete(){
        $this->getIndexList()->delItem($this->{$this->primaryAttribute});
        $this->redis()->delete($this->getDataKey());
    }
    
    public function deleteCachaItem($pk){
        $this->redis()->delete($this->getDataKey($pk));
    }
    
    /**
     * 更新MarkKey
     */
    public function updateMarkKey($pk){
        $this->redis()->set($this->getMarkKey($pk),time());
    }
    
    public function findByPk($pk){
        $this->{$this->primaryAttribute} = $pk;
        $cacheData = $this->redis()->hGetAll($this->getDataKey());
        if (!empty($cacheData)){
            $className = get_class($this);
            $auctionCache = new $className();
            $auctionCache->attributes = $cacheData;
            return $auctionCache;
        }
        return null;
    }
    
    public function findGNCByPk($pk){
        $this->{$this->primaryAttribute} = $pk;
        if (!$this->hasCacheExist($pk)){
            $auctionDao = new AuctionLiveDao();
            $auctionDaoData = $auctionDao->find($this->{$this->primaryAttribute});
            $this->attributes = $auctionDaoData->attributes;
            $this->winner_avatar = $auctionDaoData->avatar;
            $this->winner_name = $auctionDaoData->user_name;
            $this->value_price=$auctionDaoData->value_price;
            $this->end_time=$auctionDaoData->end_time;
            $this->create();
        }
        
        return $this->findByPk($pk);
    }
    
    public function findAllByPk($pks){
        $findAll = array();
        foreach ($pks as $pk){
            $findData = $this->findByPk($pk);
            if (!empty($findData)){
                array_push($findAll, $findData);
            }
            
        }
        return $findAll;
    }
    
    public function findAllGNCByPks($pks){
        $findAll = array();
        $buildPks = array();
        foreach ($pks as $pk){
            $valid = $this->hasCacheExist($pk);
            if (!$valid){
                array_push($buildPks,$pk);
            }
        }
        if (!empty($buildPks)){
            $auctionPoolAr = new AuctionPool();
            $auctionPoolDatas = $auctionPoolAr->findAllByPk($buildPks);
            if (!empty($auctionPoolDatas)){
                foreach ($auctionPoolDatas as $auctionPoolData){
                    $this->{$this->primaryAttribute} = $auctionPoolData->auction_id;
                    $auctionDao = new AuctionLiveDao();
                    $auctionDaoData = $auctionDao->find($this->{$this->primaryAttribute});
                    $this->attributes = $auctionDaoData->attributes;
                    $this->winner_avatar = $auctionDaoData->avatar;
                    $this->winner_name = $auctionDaoData->user_name;
                    $this->value_price = $auctionDao->value_price;
                    $this->create();
                }
            }
        }
        $findAll = $this->findAllByPk($pks);

        return $findAll;
    }
    
    public function findModifyAllByPkGNC($pks,$ver){        
        $findAll = array();
        $buildPks = array();
        foreach ($pks as $pk){
            $valid = $this->hasCacheExist($pk);
            if (!$valid){
                array_push($buildPks,$pk);
            }
        }
        if (!empty($buildPks)){
            $criteria=new CDbCriteria();
            $criteria->addInCondition("t.auction_id", $buildPks);
            $auctionPoolAr = new AuctionPool();
            $auctionPoolDatas = $auctionPoolAr->findAll($criteria);
            if (!empty($auctionPoolDatas)){
                foreach ($auctionPoolDatas as $auctionPoolData){
                    $this->{$this->primaryAttribute} = $auctionPoolData->auction_id;
                    $auctionDao = new AuctionLiveDao();
                    $auctionDaoData = $auctionDao->find($this->{$this->primaryAttribute});
                    $this->attributes = $auctionDaoData->attributes;
                    $this->winner_avatar = $auctionDaoData->avatar;
                    $this->winner_name = $auctionDaoData->user_name;
                    $this->value_price=$auctionDaoData->value_price;
                    $this->end_time=$auctionDaoData->end_time;
                    $this->create();
                }
            }
        }
        
        $pks = $this->hasCacheModifyPks($pks, $ver);
        
        $findAll = $this->findAllByPk($pks);
        
        return $findAll;
        
    }
    /*
    public function findModifyAllByPk($pks,$ver){
        $findAll = array();
        $pks = $this->hasCacheModifyPks($pks, $ver);
        $findAll = $this->findAllByPk($pks);
        return $findAll;
    }
    */
    public function hasCacheExist($pk){
        $this->{$this->primaryAttribute} = $pk;
        return $this->redis()->exists($this->getDataKey());
    }
    
    /**
     * 通过检测传递过来的ver是否小于redis中存的ver
     */
    public function hasCacheModify($pk,$ver){
        $this->{$this->primaryAttribute} = $pk;
        $markVer = $this->redis()->get($this->getMarkKey());
        return intval($markVer) >= $ver;
    }
    
    /**
     * 返回所有的缓存发生更改后的id数组
     */
    public function hasCacheModifyPks($pks,$ver){
        $findPks = array();
        foreach ($pks as $pk){
           $valid = $this->hasCacheModify($pk, $ver);
           if ($valid){               
               array_push($findPks,$pk);
           }
        }
        return $findPks;
    }
    
    /**
     * 更新指定id的数据
     */
    public function update($pk,$data){
        $this->{$this->primaryAttribute} = $pk;
        if ($this->hasCacheExist($pk)){
           $updateData = array_intersect_key($data,$this->attributes);
           $update = $this->redis()->hMset($this->getDataKey(),$updateData);
           $this->redis()->set($this->getMarkKey(),time());
           return $update;
        }
        return null;
    }
    
    /**
     * 设置过期时间
     */
    public function setExpire($pk,$expire=79800){
    	$expire=time()+$expire;
    	$this->{$this->primaryAttribute} = $pk;
    	if ($this->hasCacheExist($pk)){
	    	$this->redis()->expireAt($this->getDataKey(),$expire);
	    	$this->redis()->expireAt($this->getMarkKey(),$expire);
    	}
    }
}
